package com.cloudptt.api.product.func.media.light;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.SystemClock;

import com.cloudptt.api.product.log.Log;

public class LightManager implements IBreathingLight {

	private static String THIS_FILE = "LightManager";
	//	private final static  int Time = 5000;
	public final static int Level_High = 3;
	public final static int Level_Operate = 2;
	public final static int Level_Low = 1;
	public final static int Level_Own = 0;

	public final static int LED_RED = 0;
	public final static int LED_GREE = 1;
	public final static int LED_BLUE = 2;

	private Context ctx = null;
	//	private Notification lightNotification = null;
	private BreathingLightReceiver receiver = null;
	//	private SharedPreferences sp = null;
	private volatile static int level = Level_Own;
	private int blnTimer = 40;
	private boolean isLogined = false;
	private int iconId = 0;
	private Class cls = null;

	private NotificationManager manager = null;

	public LightManager(Context ctx,int iconId,Class cls)
	{
//		Log.d(THIS_FILE, "LightManager");
		this.ctx = ctx;
		this.iconId = iconId;
		this.cls = cls;
		manager = (NotificationManager) ctx.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
//		this.sp = PreferenceManager.getDefaultSharedPreferences(ctx);
//		this.sp.registerOnSharedPreferenceChangeListener(this);
//		String value = sp.getString(BaseCallConfig.Timer_BLN, "" + BaseCallConfig.Default_Timer_BLN);
//		blnTimer = CheckData.parseInt(value);
//		Log.d(THIS_FILE, "LightManager blnTimer = " + blnTimer);
	}
	public void clearResouce()
	{
		Log.d(THIS_FILE, "clearResouce");
		stopBreathingLight();
		manager = null;
		ctx = null;
	}
	public void onLogin(boolean isLogin)
	{
		if(isLogin)
		{
			stopBreathingLight();
//			if(blnTimer != 0)
//			{
//				startBreathingLight();
//			}
		}
		else
		{
//			stopBreathingLight();
		}
	}
	private void startBreathingLight()
	{
		Log.d(THIS_FILE, "startBreathingLight");
		stopBreathingLight();
		openBreathingLight();

		receiver = new BreathingLightReceiver(ctx,this);

		Intent intent =new Intent(BreathingLightReceiver.ALARM_TIMER_ACTION);
		PendingIntent sender=PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		//瀵拷婵妞傞梻锟�
		long firstime=SystemClock.elapsedRealtime();

		AlarmManager am=(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
		//娑擄拷娑擃亜鎳嗛張鐕傜礉娑撳秴浠犻惃鍕絺闁礁绠嶉幘锟�
		am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime,blnTimer * 1000, sender);
	}

	private void stopBreathingLight()
	{
		Log.d(THIS_FILE, "stopBreathingLigh");
		if(receiver != null)
		{
			receiver.clearResource();
			receiver = null;
		}
		Intent intent =new Intent(BreathingLightReceiver.ALARM_TIMER_ACTION);
		PendingIntent sender=PendingIntent.getBroadcast(ctx, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
		AlarmManager alarm=(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
		alarm.cancel(sender);
	}

	final int ID_LED=20160526;

	public void openLED(boolean isFlash, int ledColor,int lev)
	{
		Log.d(THIS_FILE, "openLED isFlash = " + isFlash + " ledColor = " + ledColor + " level = " + level + " lev = " + lev);
		if(level >= lev)
		{
			return;
		}
		level = lev;
		Notification.Builder builder = new Notification.Builder(ctx);
		builder.setContentInfo("补充内容");
		builder.setContentText("主内容区");
		builder.setContentTitle("通知标题");
		builder.setSmallIcon(iconId);
		builder.setTicker("新消息");
		builder.setAutoCancel(true);

		builder.setWhen(System.currentTimeMillis());//设置时间，设置为系统当前的时间
		Intent intent = new Intent(ctx, cls);
		PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		builder.setContentIntent(pendingIntent);
		Notification notification = builder.build();
		/**
		 * vibrate属性是一个长整型的数组，用于设置手机静止和振动的时长，以毫秒为单位。
		 * 参数中下标为0的值表示手机静止的时长，下标为1的值表示手机振动的时长， 下标为2的值又表示手机静止的时长，以此类推。
		 */
		long[] vibrates = { 0, 1000, 1000, 1000 };
		notification.vibrate = vibrates;

		/**
		 * 手机处于锁屏状态时， LED灯就会不停地闪烁， 提醒用户去查看手机,下面是绿色的灯光一 闪一闪的效果
		 */
		if(ledColor==0){
			notification.ledARGB = 0xffFF0000;
		}else if(ledColor==1){
			notification.ledARGB = 0xff00FF00;
		}else if(ledColor==2){
			notification.ledARGB = 0xff0000FF;
		}else {
			return;
		}
//		notification.ledARGB = Color.RED;// 控制 LED 灯的颜色，一般有红绿蓝三种颜色可选
		notification.ledOnMS = 1000;// 指定 LED 灯亮起的时长，以毫秒为单位
		if(!isFlash)
		{
			notification.ledOffMS = 0;
		}
		else
		{
			notification.ledOffMS = 1000;
		}
//		notification.ledOffMS = 3000;// 指定 LED 灯暗去的时长，也是以毫秒为单位
		notification.flags = Notification.FLAG_SHOW_LIGHTS;// 指定通知的一些行为，其中就包括显示
		// LED 灯这一选项
		if(manager == null && ctx != null)
		{
			manager = (NotificationManager) ctx.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		}
		if(manager != null
				&& notification != null) {
			manager.notify(ID_LED, notification);
		}
	}

	public void closeLED(int lev)
	{
		Log.d(THIS_FILE, "closeLED level = " + level + " lev = " + lev);
//		if(!isLogined)
//		{
//			return;
//		}
		if(level != lev)
		{
			return;
		}
//		Log.d(THIS_FILE, "closeLED ");
		level = Level_Own;

		NotificationManager nm = (NotificationManager) ctx.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		nm.cancel(ID_LED);
//		nm.cancelAll();

//		openBreathingLight();
	}

	public void openBreathingLight()
	{
		Log.d(THIS_FILE, "openBreathingLight level = " + level);
		if(level != Level_Own)
		{
			return;
		}
		Notification.Builder builder = new Notification.Builder(ctx);
		builder.setContentInfo("补充内容");
		builder.setContentText("主内容区");
		builder.setContentTitle("通知标题");
		builder.setSmallIcon(iconId);
		builder.setTicker("新消息");
		builder.setAutoCancel(true);

		builder.setWhen(System.currentTimeMillis());//设置时间，设置为系统当前的时间
		Intent intent = new Intent(ctx, cls);
		PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		builder.setContentIntent(pendingIntent);
		Notification notification = builder.build();
		/**
		 * vibrate属性是一个长整型的数组，用于设置手机静止和振动的时长，以毫秒为单位。
		 * 参数中下标为0的值表示手机静止的时长，下标为1的值表示手机振动的时长， 下标为2的值又表示手机静止的时长，以此类推。
		 */
		long[] vibrates = { 0, 1000, 1000, 1000 };
		notification.vibrate = vibrates;

		/**
		 * 手机处于锁屏状态时， LED灯就会不停地闪烁， 提醒用户去查看手机,下面是绿色的灯光一 闪一闪的效果
		 */
		notification.ledARGB = Color.GREEN;// 控制 LED 灯的颜色，一般有红绿蓝三种颜色可选
		notification.ledOnMS = 500;// 指定 LED 灯亮起的时长，以毫秒为单位
		notification.ledOffMS = 5000;// 指定 LED 灯暗去的时长，也是以毫秒为单位
		notification.flags = Notification.FLAG_SHOW_LIGHTS;// 指定通知的一些行为，其中就包括显示
		// LED 灯这一选项
		if(manager == null && ctx != null)
		{
			manager = (NotificationManager) ctx.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		}
		if(manager != null
				&& notification != null) {
			manager.notify(ID_LED, notification);
		}
//		this.openLED(true,LED_GREE,Level_Own);
	}

	public void closeBreathingLight()
	{
		Log.d(THIS_FILE, "closeBreathingLight level = " + level);
		if(level != Level_Own)
		{
			return;
		}
//		this.closeLED(Level_Own);

		if(manager != null) {
			manager.cancel(ID_LED);
//			manager.cancelAll();
		}
	}
}
